package com.doge.walknovel.business.home.profile.push

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.core.view.isVisible
import com.doge.walknovel.api.model.PushEnableInfo
import com.doge.walknovel.business.home.profile.PushViewModel
import com.doge.walknovel.core.BaseActivity
import com.doge.walknovel.core.account.AccountHelper
import com.doge.walknovel.core.base.LoadStatus
import com.doge.walknovel.core.base.isRefreshing
import com.doge.wnpro.databinding.ActivityPushManagementBinding
import com.doge.walknovel.utils.setOnClickCallback

class PushManagementActivity : BaseActivity() {
    private val binding by lazy { ActivityPushManagementBinding.inflate(layoutInflater) }

    private val viewModel: PushViewModel by viewModels()
    private val contents by lazy {
        arrayOf(
            binding.allThePushText, binding.chaptersUpdatedText, binding.activitiesPushText, binding.allThePush,
            binding.chaptersUpdated, binding.activitiesPush
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        binding.allThePush.setOnClickCallback {
            val value = if (binding.allThePush.tag as? Boolean != false) 0 else 1
            viewModel.requestSetEnable(value, value)
        }
        binding.chaptersUpdated.setOnClickCallback {
            val value = if (binding.chaptersUpdated.tag as? Boolean != false) 0 else 1
            viewModel.requestSetEnable(value, null)
        }
        binding.activitiesPush.setOnClickCallback {
            val value = if (binding.activitiesPush.tag as? Boolean != false) 0 else 1
            viewModel.requestSetEnable(null, value)
        }

        viewModel.loading.observe(this) {
            binding.initLoading.isVisible = it.isRefreshing()
            contents.forEach { view -> view.isVisible = !it.isRefreshing() }
        }
        viewModel.setLoading.observe(this, ::updateSetLoading)
        viewModel.enableInfo.observe(this, ::updateBtn)
        viewModel.requestEnableInfo()
    }

    private fun updateBtn(data: PushEnableInfo?) {
        if (data?.appUpdate == true) {
            binding.allThePush.toggleSwitch(data.isSelectedAll == 1)
            binding.allThePush.tag =  (data.isSelectedAll == 1)
            binding.chaptersUpdated.toggleSwitch(data.chapterUpdatePush == 1)
            binding.chaptersUpdated.tag =  (data.isSelectedAll == 1)
            binding.activitiesPush.toggleSwitch(data.activityPush == 1)
            binding.activitiesPush.tag =  (data.isSelectedAll == 1)
        } else {
            binding.allThePush.isOpened = (data?.isSelectedAll == 1)
            binding.allThePush.tag =  (data?.isSelectedAll == 1)
            binding.chaptersUpdated.isOpened = (data?.chapterUpdatePush == 1)
            binding.chaptersUpdated.tag =  (data?.isSelectedAll == 1)
            binding.activitiesPush.isOpened = (data?.activityPush == 1)
            binding.activitiesPush.tag =  (data?.isSelectedAll == 1)
        }
    }

    private fun updateSetLoading(data: Pair<Int, LoadStatus>?) {
        binding.allLoading.isVisible = data?.first == PushViewModel.UPDATE_ALL && data.second.isRefreshing()
        binding.chapterLoading.isVisible =
            data?.first == PushViewModel.UPDATE_CHAPTER && data.second.isRefreshing()
        binding.activityLoading.isVisible =
            data?.first == PushViewModel.UPDATE_ACTIVITY && data.second.isRefreshing()
    }

    companion object {
        fun start(context: Context) {
            AccountHelper.requiredLogin {
                context.startActivity(Intent(context, PushManagementActivity::class.java))
            }
        }
    }
}